module fbc_reader_mod
    use iso_fortran_env,only:real64
    implicit none
contains
    
    !> 用于静态分析,加载力边界条件, fbc_pos(作用的节点数,2): 第一列是节点,第二列是自由度(1~6)
    !> fbc_val(作用的节点数): 力加载值数组; fbc_num: 作用的节点数
    subroutine load_fbcs_ls(filename,fbc_pos,fbc_val ,fbc_num, ierr)
        implicit none
        character(len=*), intent(in) :: filename
        integer, allocatable, intent(out) :: fbc_pos(:,:)
        real(real64),allocatable,intent(out) :: fbc_val(:)
        integer, intent(out) :: fbc_num,ierr
        integer :: fileunit,i,stat
        
        ierr = 0
        open(newunit=fileunit, file=filename, status='old',action='read', iostat=stat)
        if (stat /= 0) then
            ierr = 1  ! 文件打开失败
            return
        end if
        
        ! 先统计行数
        fbc_num = 0
        do while(.true.)
            read(fileunit,*, iostat=stat) 
            if (stat /= 0) exit
            fbc_num = fbc_num + 1
        end do

        ! 分配数组内存
        allocate(fbc_pos(fbc_num,2),fbc_val(fbc_num),stat=stat)
        if (stat /= 0) then
            ierr = 2  ! 内存分配失败
            close(fileunit)
            return
        end if
        
        ! 逐行读取坐标数据
        rewind(fileunit)  ! 文件指针回到开头
        do i=1,fbc_num
            read(fileunit,*,iostat=stat) fbc_pos(i,1),fbc_pos(i,2),fbc_val(i)
            if (stat /= 0) then
                deallocate(fbc_pos,fbc_val)
                ierr = 3  ! 数据读取失败
                close(fileunit)
                write(*,'(A,I0,A)') "Error ! when read line ",i,"of fbc file failed!"
                STOP
            end if 
        end do
        ! 关闭文件
        close(fileunit)
    end subroutine load_fbcs_ls
    
    !> 用于线性动态分析,加载力加载条件, fbc_pos(作用的节点数,2): 第一列是节点,第二列是自由度(1~6)
    !> fbc_amp(作用的节点数): 力的幅值曲线编号数组; fbc_val(作用的节点数): 力加载值数组; fbc_num: 作用的节点数
    subroutine load_fbcs_dy(filename,fbc_pos,fbc_val,fbc_amp,fbc_num, ierr)
        implicit none
        character(len=*), intent(in) :: filename
        integer, allocatable, intent(out) :: fbc_pos(:,:),fbc_amp(:)
        real(real64),allocatable,intent(out) :: fbc_val(:)
        integer, intent(out) :: fbc_num,ierr
        integer :: fileunit,i,stat
        
        ierr = 0
        open(newunit=fileunit, file=filename, status='old',action='read', iostat=stat)
        if (stat /= 0) then
            ierr = 1  ! 文件打开失败
            return
        end if
        
        ! 先统计行数
        fbc_num = 0
        do while(.true.)
            read(fileunit,*, iostat=stat) 
            if (stat /= 0) exit
            fbc_num = fbc_num + 1
        end do

        ! 分配数组内存
        allocate(fbc_pos(fbc_num,2),fbc_val(fbc_num),fbc_amp(fbc_num),stat=stat)
        if (stat /= 0) then
            ierr = 2  ! 内存分配失败
            close(fileunit)
            return
        end if
        
        ! 逐行读取坐标数据
        rewind(fileunit)  ! 文件指针回到开头
        do i=1,fbc_num
            read(fileunit,*,iostat=stat) fbc_pos(i,1),fbc_pos(i,2),fbc_val(i),fbc_amp(i)
            if (stat /= 0) then
                deallocate(fbc_pos,fbc_val,fbc_amp)
                ierr = 3  ! 数据读取失败
                close(fileunit)
                write(*,'(A,I0,A)') "Error ! when read line ",i,"of fbc file failed!"
                stop ;end if ;end do
        ! 关闭文件
        close(fileunit)
    end subroutine load_fbcs_dy
end module fbc_reader_mod